/**
 * 公共区域
 */
import dataRouter from 'src/router/dataRouter.js'
import { usermyself } from 'api/txEquipment/index'
let copyRouter = []

/**
 * 路由文件过滤
 *
 */
async function filterRouterFile (getters) {
  window.localStorage.user_type = getters.getRoleList.roleData.user_type
  if (getters.getRoleList.roleData.user_type === 1) {
    copyRouter = dataRouter.basisRouter
    return dataRouter.basisRouter
  } else {
    let arr = dataRouter.basisRouter.map(item => {
      if (!(getters.getRoleList.roleData.nodes.indexOf(item.meta.name) !== -1)) item.hidden = true
      return item
    })

    var lisArr1 = arr.filter(item => {
      return !item.hidden
    })

    copyRouter = lisArr1
    // console.log(123, JSON.parse(JSON.stringify(lisArr1)))
    return lisArr1
  }
}

const state = {
  roleData: {} // 用户信息
}

const getters = {
  getNetType: state => { // 获取系统类型
    if (state.roleData.mobile) {
      return 1
    }
    return ''
  },
  getRoleList: state => {
    return state
  } // 获取权限列表
}

const mutations = {
  SET_ROLEDATA (state, payload) { // 用户信息
    state.roleData = payload
  }
}

const actions = {
  setResetData ({ commit }) { // 重置路由信息
    commit('SET_ROLEDATA', {})
  },
  setRoleData ({ commit, getters }) {
    return new Promise(async (resolve, reject) => {
      try {
        let res = await usermyself()
        localStorage.setItem('txUserInfo', JSON.stringify(res.data))
        commit('SET_ROLEDATA', res.data)
        resolve(filterRouterFile(getters))
      } catch (err) {
        reject(err)
      }
    })
  },
  setSystemInfo ({ getters, commit }) { // 此方法用于处理顶部信息与设置模块
    return new Promise(async (resolve, reject) => {
      try {
        let params = {
          'copyRouter': copyRouter
        }
        commit('SET_SYSTEMINFO', {
          ...params
        })
        resolve({
          ...params
        })
      } catch (err) {
        reject(err)
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
